package com.view.houseon;



import com.controller.houseon.ControlHouseON;
import com.presentacion.houseon.R;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

	Button btnC;
	
	TextView txtuser, txtpass;
	
	
	static final int id_cliente=1;
	
	ImageButton ibtn_website;
	ImageButton ibtn_information;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_login);
		
		btnC=(Button)findViewById(R.id.btnConectar);
		btnC.setOnClickListener(this);
		

		
		TextView txtOlvido=(TextView)findViewById(R.id.txt_olvido);
		txtOlvido.setText(Html.fromHtml(getString(R.string.olvido)));
		Linkify.addLinks(txtOlvido, Linkify.ALL);
		txtOlvido.setMovementMethod(LinkMovementMethod.getInstance());
		
		
		txtuser=(TextView)findViewById(R.id.txtUsuario);
		txtpass=(TextView)findViewById(R.id.txtPassword);
		
		ibtn_information=(ImageButton)findViewById(R.id.ibtnInformacion);
		ibtn_information.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(ListaEscenariosActivity.this, String.valueOf("logout.."), Toast.LENGTH_SHORT).show();
				 Intent i = new Intent(LoginActivity.this, InformacionHouseONActivity.class);
				 startActivity(i);
			}
		});
		
		ibtn_website=(ImageButton)findViewById(R.id.ibtnHome);
		ibtn_website.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Toast.makeText(ListaEscenariosActivity.this, String.valueOf("inicio.."), Toast.LENGTH_SHORT).show();
				String url = "http://www.houseon.net";
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(url));
				startActivity(i);
			}
		});
		
	}

	  public  void salir(){
	       Intent i = new Intent(); 
	                  i.setAction( Intent.ACTION_MAIN); 
	                  i.addCategory( Intent.CATEGORY_HOME); 
	                  i.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP); 
	                  startActivity(i); 
	                  android.os.Process.killProcess( android.os.Process.myPid()); 
	                  finish();
	  }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==btnC){
			
			String us= txtuser.getText().toString().trim();
			String ps= txtpass.getText().toString().trim();
			
			if(!us.equals("") && !ps.equals("")){
				ControlHouseON.getInstancia().CurrentUsuario =ControlHouseON.getInstancia().validaUsuario(us,ps);
			
				
				if(ControlHouseON.getInstancia().CurrentUsuario!=null){
					
					// acceso a interfaz cliente
					
					if(ControlHouseON.getInstancia().CurrentUsuario.getTipoUsuario().getIdTipoUsuario()==id_cliente){
					Toast.makeText(LoginActivity.this, String.valueOf("conectando.."), Toast.LENGTH_SHORT).show();
					Intent i= new Intent(LoginActivity.this, ListaCasaActivity.class);
					startActivity(i);
				
					}
					else{
						
						AlertDialog.Builder builder = new AlertDialog.Builder(this);
						builder.setMessage("Usuario no es Cliente")
						       .setCancelable(false)
						       .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
						           public void onClick(DialogInterface dialog, int id) {
						                dialog.cancel();
						           }
						       });
						      
						AlertDialog alert = builder.create();
						alert.show();
					
						
					}
				}
				else {
					AlertDialog.Builder builder = new AlertDialog.Builder(this);
					builder.setMessage("Credenciales incorrectas")
					       .setCancelable(false)
					       .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					      
					AlertDialog alert = builder.create();
					alert.show();
			    
					}
				}
			else{
				
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setMessage("Ingrese Datos")
				       .setCancelable(false)
				       .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dialog.cancel();
				           }
				       });
				      
				AlertDialog alert = builder.create();
				alert.show();
				
			}
		}

		
	}

}
